﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib.Forms;
using WFNetLib.GSMModem;
using System.Threading;

namespace 自消费
{
    public partial class 配置设备 : Form
    {
        public 配置设备()
        {
            InitializeComponent();
        }
        /************************************************************************/
        /* 搜索新增设备                                                         */
        /************************************************************************/
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string[] ports = SerialPort.GetPortNames();
            foreach (string str in ports)
            {
                bool bAdd = true;
                foreach (ListViewItem lvi in listView1.Items)
                {
                    if (str == lvi.SubItems[1].Text)
                    {
                        bAdd = false;
                        break;
                    }
                }
                if (bAdd)
                {
                    GetDevice(str);
                }
            }
            this.Cursor = Cursors.Default;
            MessageBox.Show("搜索完成");
        }

        private void 配置设备_Load(object sender, EventArgs e)
        {
            string sql="select * from device";
            DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql);
            listView1.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem lvi=listView1.Items.Add((i + 1).ToString());
                DataRow dr=dt.Rows[i];
                lvi.SubItems.Add("COM" + dr["commport"].ToString());
                lvi.SubItems.Add(GetDeviceName(Convert.ToInt32(dr["type"])));
                if (Convert.ToBoolean(dr["enabled"]))
                    lvi.SubItems.Add("启用");
                else
                    lvi.SubItems.Add("禁用 ");
            }
        }
        string GetDeviceName(int type)
        {
            switch (type)
            {
                case 4:
                    return "华为";
            }
            return "未知";
        }
        void GetDevice(string str)
        {            
            int type;
            //string baudrate = "115200";
            SerialPort com = new SerialPort(str, 115200);
            com.RtsEnable = true;
            com.ReadTimeout = 300;
            ATCommand atComm = new ATCommand(com);
            try
            {
                com.Open();
                com.ReadTimeout = 300;
                if (atComm.SendAT("ATE0"))
                {
                    string strCGSN;//产品序列号 
                    string strGMM;//产品序列号 
                    string strATI;//产品序列号 
                    if (atComm.SendAT("AT+GMM"))
                    {
                        strGMM = atComm.ATResult.Replace("\r\n", "");
                    }
                    else
                        strGMM = "未知GMM";
                    if (atComm.SendAT("ATI"))
                    {
                        strATI = atComm.ATResult.Replace("\r\n", "");
                    }
                    else
                        strATI = "未知ATI";
                    if (atComm.SendAT("AT+CGSN"))
                    {
                        strCGSN = atComm.ATResult.Replace("\r\n", "");
                    }
                    else
                    {
                        strCGSN = "未知CGSN";
                    }
                    switch (strGMM)
                    {
                        case "GTM900B":
                            type = 4;
                            break;
                        case " MULTIBAND  900E  1800 ":
                            type = 3;
                            break;
                        default:
                            type = 0;
                            break;
                    }
                    deviceData d = new deviceData();
                    d.baudrate = 115200;
                    d.commport = Convert.ToInt32(str.Replace("COM", ""));
                    d.imei = strCGSN;
                    d.type = type;
                    d.seqnum = listView1.Items.Count + 1;
                    if (deviceDataDBOption.Insert(d) != 1)
                    {
                        MessageBox.Show("添加" + str + "失败");
                    }
                    else
                    {
                        ListViewItem lvi = listView1.Items.Add((listView1.Items.Count+1).ToString());
                        lvi.SubItems.Add(str);
                        switch (type)
                        {
                            case 4:
                                lvi.SubItems.Add("华为");
                                break;
                            case 3:
                                lvi.SubItems.Add("WAVECOM");
                                break;
                            default:
                                 lvi.SubItems.Add("未知");
                                break;
                        }                                             
                        lvi.SubItems.Add("启用");
                    }
                }
                com.Close();
            }
            catch
            {
                if (com.IsOpen)
                    com.Close();
            }            
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            listView1.Items.Clear();
            string sql = "delete from device";
            WFNetLib.ADO.AccessOP.NonQuerySQL(sql);
            string[] ports = SerialPort.GetPortNames();
            foreach (string str in ports)
            {               
                GetDevice(str);
            }
            this.Cursor = Cursors.Default;
            MessageBox.Show("搜索完成");
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                e.Cancel = true;
                return;
            }
            ListViewItem lvi=listView1.SelectedItems[0];
            string str=lvi.SubItems[3].Text;
            if (str == "启用")
                contextMenuStrip1.Items[0].Text = "禁用设备";
            else
                contextMenuStrip1.Items[0].Text = "启用设备";
        }

        private void 禁用设备ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem lvi = listView1.SelectedItems[0];
            string str = lvi.SubItems[3].Text;
            string sql;
            if (str == "启用")
            {
                sql = "update device set enabled=0 where commport=" + lvi.SubItems[1].Text.Replace("COM", "");
                WFNetLib.ADO.AccessOP.NonQuerySQL(sql);
                lvi.SubItems[3].Text = "禁用";
            }
            else
            {
                sql = "update device set enabled=1 where commport=" + lvi.SubItems[1].Text.Replace("COM", "");
                WFNetLib.ADO.AccessOP.NonQuerySQL(sql);
                lvi.SubItems[3].Text = "启用";
            }
        }

        private void 删除设备ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem lvi = listView1.SelectedItems[0];
            string sql;
            sql = "delete from device where commport=" + lvi.SubItems[1].Text.Replace("COM", "");
            WFNetLib.ADO.AccessOP.NonQuerySQL(sql);
            listView1.Items.RemoveAt(lvi.Index);
        }

    }   
}
